package cinema.service;

import cinema.model.*;
import cinema.util.*;
import java.util.*;

public class MovieService {
    private static final List<Movie> movies = new ArrayList<>();

    public static void manageMovies() {
        while (true) {
            System.out.println("\n===影片管理===");
            System.out.println("1. 添加 2. 修改 3. 删除 4. 查询 5. 返回");
            switch (InputUtil.readInt("请选择: ")) {
                case 1 -> {
                    String t = InputUtil.readLine("片名: ");
                    String d = InputUtil.readLine("导演: ");
                    String a = InputUtil.readLine("主演: ");
                    String p = InputUtil.readLine("简介: ");
                    int dur = InputUtil.readInt("时长: ");
                    double r = InputUtil.readDouble("评分(0-10): ");
                    movies.add(new Movie(t,d,a,p,dur,r));
                }
                case 2 -> System.out.println("修改功能略，可删除后添加");
                case 3 -> {
                    String del = InputUtil.readLine("删除片名: ");
                    movies.removeIf(m -> m.getTitle().equals(del) && !ShowtimeService.isScheduled(m));
                }
                case 4 -> listMovies();
                case 5 -> { return; }
            }
        }
    }

    public static void listMovies() {
        movies.forEach(Movie::print);
    }

    public static List<Movie> getAll() { return movies; }
    public static Movie getByTitle(String title) {
        return movies.stream().filter(m -> m.getTitle().equals(title)).findFirst().orElse(null);
    }
}